{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0abcad0a",
   "metadata": {},
   "source": [
    "## 2 python基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81eb8450",
   "metadata": {},
   "source": [
    "### 2.1 匿名函数与map方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4767f058",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 匿名函数\n",
    "[(lambda x : 2*x)(i) for i in range (5)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7f5ed253",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x :2*x,range(5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "72d180f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明-18', '小红-19', '李明-17']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 学生练习\n",
    "name = ['小明','小红','李明']\n",
    "age = [18,19,17]\n",
    "\n",
    "#希望得到效果：['小明_18',.....]\n",
    "\n",
    "#正确方法\n",
    "list(map(lambda x,y:x+'-'+str(y),name,age))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a375a81",
   "metadata": {},
   "source": [
    "### 2.2 zip对象与enumerate方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d965f9e",
   "metadata": {},
   "source": [
    "* zip 函数能够把多个可迭代对象打包成一个元组构成的可迭代对象，它返回了一个zip 对象，通过 tuple ， list 可以得到相应的打包结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "225381fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j')]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1, L2, L3 = list('abc'), list('def'), list('hij')\n",
    "list(zip(L1,L2,L3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ffddc3ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j'))"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tuple(zip(L1,L2,L3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b770a9f9",
   "metadata": {},
   "source": [
    "* 往往会在循环迭代的时候使用得到 zip 函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1a874b32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a d h\n",
      "b e i\n",
      "c f j\n"
     ]
    }
   ],
   "source": [
    "for i, j,k in zip(L1,L2,L3):\n",
    "    print(i,j,k)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b47f689a",
   "metadata": {},
   "source": [
    "* enumerate 是一种特殊的打包，他在迭代时绑定迭代元素的遍历序号："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2ed4daf3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 a\n",
      "1 b\n",
      "2 c\n",
      "3 d\n"
     ]
    }
   ],
   "source": [
    "L = list('abcd')\n",
    "\n",
    "for index ,value in enumerate(L):\n",
    "    print (index,value)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "081a98f0",
   "metadata": {},
   "source": [
    "* 用 zip 对象也能够简单地实现这个功能："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "56f29de9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 a\n",
      "1 b\n",
      "2 c\n",
      "3 d\n"
     ]
    }
   ],
   "source": [
    " for index, value in zip(range(len(L)), L):\n",
    "         print(index, value)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65549f57",
   "metadata": {},
   "source": [
    "* 当需要对两个列表建立字典映射时，可以利用 zip 对象："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "76049eb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 'd', 'b': 'e', 'c': 'f'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " dict(zip(L1, L2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2045bc3",
   "metadata": {},
   "source": [
    "* 既然有了压缩函数，那么 Python 也提供了 * 操作符和 zip 联合使用来进行解压操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c63f8491",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j')]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zipped = list(zip(L1, L2, L3))\n",
    "zipped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "71a9dee2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 'b', 'c'), ('d', 'e', 'f'), ('h', 'i', 'j')]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(zip(*zipped))  # 三个元组分别对应原来的列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7ccd488",
   "metadata": {},
   "source": [
    "# 3 文件的读取与写入"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8f83df4",
   "metadata": {},
   "source": [
    "## 3.1 数据读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6bf3e960",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "83286154",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col1</th>\n",
       "      <th>col2</th>\n",
       "      <th>col3</th>\n",
       "      <th>col4</th>\n",
       "      <th>col5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>1.4</td>\n",
       "      <td>apple</td>\n",
       "      <td>2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>3.4</td>\n",
       "      <td>banana</td>\n",
       "      <td>2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>c</td>\n",
       "      <td>2.5</td>\n",
       "      <td>orange</td>\n",
       "      <td>2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>d</td>\n",
       "      <td>3.2</td>\n",
       "      <td>lemon</td>\n",
       "      <td>2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   col1 col2  col3    col4      col5\n",
       "0     2    a   1.4   apple  2020/1/1\n",
       "1     3    b   3.4  banana  2020/1/2\n",
       "2     6    c   2.5  orange  2020/1/5\n",
       "3     5    d   3.2   lemon  2020/1/7"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('C:/Users/pc/Desktop/data_analysis-master/data/my_csv.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e388a1c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col1,col2,col3,col4,col5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2,a,1.4,apple,2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3,b,3.4,banana,2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6,c,2.5,orange,2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5,d,3.2,lemon,2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  col1,col2,col3,col4,col5\n",
       "0   2,a,1.4,apple,2020/1/1\n",
       "1  3,b,3.4,banana,2020/1/2\n",
       "2  6,c,2.5,orange,2020/1/5\n",
       "3   5,d,3.2,lemon,2020/1/7"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_table('C:/Users/pc/Desktop/data_analysis-master/data/my_csv.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "73f49b81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col2</th>\n",
       "      <th>col3</th>\n",
       "      <th>col4</th>\n",
       "      <th>col5</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>col1</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>1.4</td>\n",
       "      <td>apple</td>\n",
       "      <td>2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b</td>\n",
       "      <td>3.4</td>\n",
       "      <td>banana</td>\n",
       "      <td>2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>c</td>\n",
       "      <td>2.5</td>\n",
       "      <td>orange</td>\n",
       "      <td>2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>d</td>\n",
       "      <td>3.2</td>\n",
       "      <td>lemon</td>\n",
       "      <td>2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     col2  col3    col4      col5\n",
       "col1                             \n",
       "2       a   1.4   apple  2020/1/1\n",
       "3       b   3.4  banana  2020/1/2\n",
       "6       c   2.5  orange  2020/1/5\n",
       "5       d   3.2   lemon  2020/1/7"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_excel('C:/Users/pc/Desktop/data_analysis-master/data/my_excel.xlsx',index_col='col1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "495859e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>col1</td>\n",
       "      <td>col2</td>\n",
       "      <td>col3</td>\n",
       "      <td>col4</td>\n",
       "      <td>col5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>1.4</td>\n",
       "      <td>apple</td>\n",
       "      <td>2020/1/1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>3.4</td>\n",
       "      <td>banana</td>\n",
       "      <td>2020/1/2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6</td>\n",
       "      <td>c</td>\n",
       "      <td>2.5</td>\n",
       "      <td>orange</td>\n",
       "      <td>2020/1/5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>d</td>\n",
       "      <td>3.2</td>\n",
       "      <td>lemon</td>\n",
       "      <td>2020/1/7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      0     1     2       3         4\n",
       "0  col1  col2  col3    col4      col5\n",
       "1     2     a   1.4   apple  2020/1/1\n",
       "2     3     b   3.4  banana  2020/1/2\n",
       "3     6     c   2.5  orange  2020/1/5\n",
       "4     5     d   3.2   lemon  2020/1/7"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_excel('C:/Users/pc/Desktop/data_analysis-master/data/my_excel.xlsx',header = None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b64ee7e",
   "metadata": {},
   "source": [
    "* 重要文件读取方法（特殊格式文件）\n",
    "    * 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "5ba98199",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-54-1fea4cd64f35>:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.\n",
      "  pd.read_table('C:/Users/pc/Desktop/data_analysis-master/data/my_table_special_sep.txt',sep='\\|\\|\\|\\|')\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col1</th>\n",
       "      <th>col2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>TS</td>\n",
       "      <td>This is an apple.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>GQ</td>\n",
       "      <td>My name is Bob.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>WT</td>\n",
       "      <td>Well done!</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>PT</td>\n",
       "      <td>May I help you?</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  col1                 col2\n",
       "0   TS    This is an apple.\n",
       "1   GQ      My name is Bob.\n",
       "2   WT           Well done!\n",
       "3   PT      May I help you?"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_table('C:/Users/pc/Desktop/data_analysis-master/data/my_table_special_sep.txt',sep='\\|\\|\\|\\|')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eebc816c",
   "metadata": {},
   "source": [
    "* 数据存入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd60627a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
